package com.otto.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.otto.reggie.common.R;
import com.otto.reggie.entity.Employee;
import com.otto.reggie.mapper.EmployeeMapper;
import com.otto.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import javax.servlet.http.HttpServletRequest;


/**
 * 作者：otto
 * 时间：2022-07-19 16:42:03
 */
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    public static Employee employee;

    /**
     * 后台员工登录
     *
     * @param request
     * @param employee
     * @return
     */
    @Override
    public R<Employee> login(HttpServletRequest request, Employee employee) {

        log.info("员工登陆方法收到参数  {}，{}", request.toString(), employee.toString());
        //获取前端收到的用户名和密码（md5加密处理）
        String name = employee.getUsername();
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //构建查询条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //查询对应用户名员工
        queryWrapper = queryWrapper.eq(Employee::getUsername, name);
        Employee user = this.getOne(queryWrapper);
        //不存在此用户名的员工
        if (user == null) {
            return R.error("不存在此用户");
        }
        //查询对应用户名员工的密码
        queryWrapper = queryWrapper.eq(Employee::getPassword, password);
        user = this.getOne(queryWrapper);
        //密码错误
        if (user == null) {
            return R.error("密码错误");
        }
        //员工状态为禁用
        if (user.getStatus() == 0) {
            return R.error("该用户被禁用");
        }
        request.getSession().setAttribute("employee", user.getId());
        log.info("对象id: {}，存入session", user.getId());
        //成功查询
        return R.success(user);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @Override
    public R<String> logout(HttpServletRequest request) {
        log.info("移除session中保存的对象id {}", request.getSession().getAttribute("employee"));
        request.getSession().removeAttribute("employee");
        return R.success("退出登陆成功");
    }

    /**
     * 新增员工
     *
     * @param employee
     * @return
     */
    @Override
    public R<String> add(HttpServletRequest request, Employee employee) {
        log.info("收到员工信息 {}", employee.toString());
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        /*employee.setUpdateTime(LocalDateTime.now());
        employee.setCreateTime(LocalDateTime.now());
        employee.setCreateUser((Long) request.getSession().getAttribute("employee"));
        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));*/
        this.save(employee);
        String msg = "保存员工 " + employee.getName() + " 成功";
        log.info(msg);
        return R.success(msg);
    }

    /**
     *员工信息 分页条件查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public R<Page> page(int page, int pageSize, String name) {

        log.info("接收到前端数据 {} ， {} ，{}", page, pageSize, name);
        //构造分页构造器
        Page pageInfo = new Page(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        this.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 启用or禁用员工账号
     *
     * @param employee
     * @return
     */
    public R<String> update(HttpServletRequest request, Employee employee) {
        if (employee.getId() == 1) {
            return R.error("不能修改管理员");
        }
        log.info("收到参数id： {} ,{}", employee.getId(), employee.getStatus());

        Long id = Thread.currentThread().getId();
        log.info("线程id为 {}",id);
        this.updateById(employee);
        return R.success("修改状态成功");
    }

    /**
     * 根据id查询员工信息
     *
     * @param id
     * @return
     */
    public R<Employee> getById1(Long id) {
        Employee employee = this.getById(id);
        if (employee!=null) {
            return R.success(employee);
        }
        return R.error("查询失败");
    }
}
